﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using System.Xml;
using Autodesk.Revit.UI;
using YArchitech.Controls;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using HYCodeBase.WinForm;

namespace HyMaterialStatistic
{
	public partial class DlgSpecialtyStatisticTable : System.Windows.Forms.Form
	{
		public DlgSpecialtyStatisticTable(ExternalCommandData commandData, ExternalEvent exEvent, GridRequestHandlerParent handler, WindowHandle wndHandler, MEP_SPECIALITY speciality, DlgMaterialStatisticParent dlgFilter, MepGridManagerParent gridMag, List<string> lstGridType)
		{
			this.InitializeComponent();
			this.m_cmmdData = commandData;
			this.m_ExEvent = exEvent;
			this.m_Handler = handler;
			this._hWndRevit = wndHandler;
			this.m_Speciality = speciality;
			this.m_dlgFilter = dlgFilter;
			this.m_gridManager = gridMag;
			switch (speciality)
			{
			case MEP_SPECIALITY.Hvac:
				this.txtGridName.Text = "暖通";
				break;
			case MEP_SPECIALITY.Plumbing:
				this.txtGridName.Text = "给排水";
				break;
			case MEP_SPECIALITY.Electric:
				this.txtGridName.Text = "电气";
				break;
			}
			foreach (string item in lstGridType)
			{
				this.cmbGridType.Items.Add(item);
			}
			this.cmbGridType.Text = "材料表";
			this.InitGridSetInfo();
		}

		private void InitGridSetInfo()
		{
			DlgColumnInfoSet dlgColumnInfoSet = new DlgColumnInfoSet(this.m_Speciality, this.cmbGridType.Text);
			this.m_gridSetInfo = dlgColumnInfoSet.m_gridSetInfo;
		}

		private void DlgSpecialtyStatisticTable_FormClosed(object sender, FormClosedEventArgs e)
		{
			if (this.m_ExEvent != null && this.m_Handler != null)
			{
				this.m_ExEvent.Dispose();
				this.m_ExEvent = null;
				this.m_Handler = null;
			}
			this.m_dlgFilter.Close();
		}

		private void btnDraw_Click(object sender, EventArgs e)
		{
			this.MakeRequest(GridRequestId.MainDraw);
		}

		public void btnDraw_Click()
		{
			if (this.m_dlgFilter.lstDicElems.Count <= 0)
			{
				YJKMessageBox.Show("没有统计数据");
				return;
			}
			this.SetCurGridLegend();
			base.Hide();
			this.m_gridManager.DrawGridToRevit(this.m_cmmdData, this, this.m_dlgFilter);
			base.Close();
		}

		private void SetCurGridLegend()
		{
			string text = "";
			switch (this.m_Speciality)
			{
			case MEP_SPECIALITY.Hvac:
				text = Path.Combine(Product.InstallLocation, "Templates", "HvacTempletlFile");
				break;
			case MEP_SPECIALITY.Plumbing:
				text = Path.Combine(Product.InstallLocation, "Templates", "PlumbingTempletlFile");
				break;
			case MEP_SPECIALITY.Electric:
				text = Path.Combine(Product.InstallLocation, "Templates", "ElectricTempletlFile");
				break;
			}
			text = text + "\\" + this.cmbGridType.Text + ".hymb";
			XmlDocument xmlDocument = new XmlDocument();
			xmlDocument.Load(text);
			foreach (object obj in xmlDocument.DocumentElement.SelectSingleNode("Cells").SelectNodes("Cell"))
			{
				XmlNode xmlNode = (XmlNode)obj;
				if (xmlNode.InnerXml == "图例" || xmlNode.InnerText == "图形符号")
				{
					this.m_gridSetInfo.Legend = true;
					return;
				}
			}
			this.m_gridSetInfo.Legend = false;
		}

		private void btnExcel_Click(object sender, EventArgs e)
		{
			this.MakeRequest(GridRequestId.MainExport);
		}

		public void btnExcel_Click()
		{
			if (this.m_dlgFilter.lstDicElems.Count <= 0)
			{
				YJKMessageBox.Show("没有统计数据");
				return;
			}
			SaveFileDialog saveFileDialog = new SaveFileDialog();
			saveFileDialog.FileName = this.txtGridName.Text;
			saveFileDialog.Filter = "Excel文件|*.xls|Excel文件|*.xlsx|盈建科表格文件|*.hybg";
			if (saveFileDialog.ShowDialog() == DialogResult.OK)
			{
				this.m_strFilePath = saveFileDialog.FileName;
				this.m_eFileType = saveFileDialog.FilterIndex;
				this.SetCurGridLegend();
			}
			base.Hide();
			if (!this.m_gridManager.ExportToExcel(this.m_cmmdData, this, this.m_dlgFilter))
			{
				return;
			}
			base.Close();
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		private void btnSet_Click(object sender, EventArgs e)
		{
			DlgColumnInfoSet dlgColumnInfoSet = new DlgColumnInfoSet(this.m_Speciality, this.cmbGridType.Text);
			if (dlgColumnInfoSet.ShowDialog() == DialogResult.OK)
			{
				this.m_gridSetInfo = dlgColumnInfoSet.m_gridSetInfo;
				this.m_gridSetInfo.SetColWidth = true;
			}
		}

		private void cmbGridType_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.txtGridName.Text = this.cmbGridType.Text;
		}

		private void btnFilter_Click(object sender, EventArgs e)
		{
			base.Hide();
			this.m_dlgFilter.bLink = this.chkLink.Checked;
			if (this.m_dlgFilter.m_mainSetDlg == null)
			{
				this.m_dlgFilter.m_mainSetDlg = this;
			}
			this.m_dlgFilter.Show(this._hWndRevit);
		}

		public void btnFilter_Click()
		{
			base.Hide();
			this.m_dlgFilter.bLink = this.chkLink.Checked;
			if (this.m_dlgFilter.m_mainSetDlg == null)
			{
				this.m_dlgFilter.m_mainSetDlg = this;
			}
			this.m_dlgFilter.Show();
		}

		private void DlgSpecialtyStatisticTable_Load(object sender, EventArgs e)
		{
			//base.Icon = Resource.YArchitechIcon;
		}

		private void MakeRequest(GridRequestId request)
		{
			this.m_Handler.Request.Make(request);
			this.m_ExEvent.Raise();
		}

		private void DlgSpecialtyStatisticTable_KeyPress(object sender, KeyPressEventArgs e)
		{
			if (e.KeyChar == '\u001b')
			{
				base.Close();
			}
		}

		private const int USER = 1024;

		public const int WM_MYMESSAGE = 1025;

		private string m_strPreviewPath = Path.Combine(Product.InstallLocation, "Templates", "HvacTempletlFile");

		private MEP_SPECIALITY m_Speciality = MEP_SPECIALITY.Hvac;

		public ExternalCommandData m_cmmdData;

		public GridRequestHandlerParent m_Handler;

		public ExternalEvent m_ExEvent;

		private MepGridManagerParent m_gridManager;

		public GridSetInfo m_gridSetInfo = new GridSetInfo();

		public DlgMaterialStatisticParent m_dlgFilter;

		public int m_eFileType;

		public string m_strFilePath = "";

		public WindowHandle _hWndRevit;
	}
}
